package com.jjrwa.from;


import com.jjrwa.component.BaseFrame;
import com.jjrwa.transform.IButtonEvent;
import com.jjrwa.transform.IClassTable;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class BuildClassTable extends BaseFrame {
    private JPanel panel1;
    private JButton cancelButton;
    private JButton OKButton;
    private JTextField classNameText;
    private JTextField descriptionText;
    private JButton fieldsButton;
    private JLabel classNameLabel;
    private JLabel descriptionLabel;


    public BuildClassTable(IClassTable classTable, IButtonEvent okButtonEvent, IButtonEvent fieldsButtonEvent) {
        this.classNameText.setText(classTable.getClassName());
        this.descriptionText.setText(classTable.getClassDescription());

        JFrame jFrame = this.windowCenter(classTable.getTitle(), this.panel1);
        jFrame.getRootPane().setDefaultButton(OKButton);

        fieldsButton.setVisible(fieldsButtonEvent.isShowButtonName());
        if (fieldsButtonEvent.buttonName() != null) {
            fieldsButton.setText(fieldsButtonEvent.buttonName());
//            fieldsButton.updateUI();
        }

        fieldsButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                fieldsButtonEvent.handle(e);
            }
        });

        OKButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                boolean handle = okButtonEvent.handle(e, jFrame, classNameText, descriptionText);
                if (handle) {
                    jFrame.dispose();
                }
            }
        });

        cancelButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jFrame.dispose();
            }
        });
    }
}
